package org.fuyd.manager;

import java.util.List;

import org.fuyd.dao.CustomerDao;
import org.fuyd.dao.LogDao;
import org.fuyd.exception.CustomerExistException;
import org.fuyd.model.Customer;
import org.fuyd.model.Log;

public class CustomerManagerImpl implements CustomerManager {
  private CustomerDao customerDao;
  private LogDao logDao;

  public void add(Customer customer) throws CustomerExistException {
    boolean exist = customerDao.exist(customer.getUsername());
    if (exist) {
      throw new CustomerExistException("用户名已存在--->" + customer.getUsername());
    }
    this.customerDao.add(customer);
    Log log = new Log();
    log.setMessage("添加了一名用户，用户名为：" + customer.getUsername());
    this.logDao.add(log);
  }

  public boolean exist(String username) {
    return this.customerDao.exist(username);
  }

  public List<Customer> findAll() {
    return this.customerDao.findAll();
  }

  public Customer findById(int id) {
    return this.customerDao.findById(id);
  }

  public CustomerDao getCustomerDao() {
    return customerDao;
  }

  public LogDao getLogDao() {
    return logDao;
  }

  public boolean login(String username, String password) {
    return this.customerDao.exit(username, password);
  }

  public void setCustomerDao(CustomerDao customerDao) {
    this.customerDao = customerDao;
  }

  public void setLogDao(LogDao logDao) {
    this.logDao = logDao;
  }

}
